﻿using AppFramework.Logger;
using APS_Define_W32;
using APS168_W64;
using PerpheralDevice.Exceptions;
using PerpheralDevice.LeadShine;
using PerpheralDevice.Units;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace PerpheralDevice.AdLink;


public class AdLinkCardAMP208C : PerpDevIO, IPerpDevMotor, IPerpDevSwithRO,IPerpDevSwitchRW {

    protected int _BoardNO = 0;
    protected string _BoardMN;

    protected int mfDiCount = 16;
    protected int mfDoCount = 16;
    protected int mfAxisCount = 4;

    private readonly ILogger _logger;
    
     public AdLinkCardAMP208C(string devID, string devName, string devParamSerialized) {
        this.PerpDevID = devID;
        this.PerpDevName = devName;
        this.IsMocking = this.Initialize();
        if (this.GetType() == typeof(AdLinkCardAMP208C)) {
            this.InitializeUnitList();
        }
    }

    protected override bool Initialize() {
        bool retValue = false;
        try {
            int tempAxisID = 0;
            int tempTotalAxisNum = 0;
            int tempCardName = APS168x64CardContainer.GetInstance().GetCardName(this._BoardNO);
            APS168.APS_get_first_axisId(this._BoardNO, ref tempAxisID, ref tempTotalAxisNum);
            if (tempCardName != (int)APS_Define.DEVICE_NAME_AMP_20408C || tempTotalAxisNum != 8) {
                this._logger?.Error($"{typeof(AdLinkCardAMP208C).FullName}初始化失败, 卡号参数:{this._BoardNO}");
            }
        } catch (Exception ex) {
            this._logger?.Error($"{typeof(AdLinkCardAMP208C).FullName}初始化失败, 卡号参数:{this._BoardNO}", ex);
            retValue = false;
        }

        return retValue;
    }

    protected override void InitializeUnitList() {

        this.mfPerpDevUnitList.Clear();
        for (int i = 0; i < this.mfAxisCount; i++) {
            this.mfPerpDevUnitList.Add(new AdLinkMotor(this, i));
        }
        for (int i = 0; i < this.mfDiCount; i++) {
            this.mfPerpDevUnitList.Add(new AdLinkSwitchRO(this, i));
        }
        for (int i = 0; i < this.mfDiCount; i++) {
            this.mfPerpDevUnitList.Add(new AdLinkSwitchRW(this, i));
        }

        #region 轮询 IO

        this.mfPollingTask = Task.Factory.StartNew(() => {
            while(this.mfIsPolling) {
                foreach (var tempItem in this.Unit<SwitchRO>()) {
                    tempItem.View();
                }
                System.Threading.Thread.Sleep(5);
            }
        }, TaskCreationOptions.LongRunning);

        #endregion

    }

    public void LoadConfigFileDefault(string filePath) {
        int retCode = APS168.APS_load_param_from_file(filePath);
        if ((APS_Define)retCode != APS_Define.ERR_NoError) {
            throw new PerpDevExceptionConstructArgument($"{typeof(AdLinkCardAMP204C).FullName}.{nameof(LoadConfigFileDefault)}加载配置文件({filePath})异常");
        }
    }

    public ICollection<Motor> MotorList => this.mfPerpDevUnitList.Where(it=>it is Motor).Select(it=> (it as Motor)!).ToList();
    public ICollection<SwitchRW> SwitchRWList => this.mfPerpDevUnitList.Where(it => it is SwitchRW).Select(it => (it as SwitchRW)!).ToList();
    public ICollection<SwitchRO> SwitchROList => this.mfPerpDevUnitList.Where(it => it is SwitchRO).Select(it => (it as SwitchRO)!).ToList();

}


